using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace QueryBuilder.Controls
{
    /// <summary>
    /// 
    /// </summary>
    [TemplatePart(Name = "ContentPresenter", Type = typeof (ContentPresenter))]
    [TemplatePart(Name = "Button", Type = typeof(Button))]
    public class DataField : ContentControl
    { 
        public static DependencyProperty IsInEditModeProperty = DependencyProperty.Register("IsInEditMode", typeof (bool), typeof (DataField), null);

        /// <summary>
        /// control type.
        /// </summary>
        public Func<object> DisplayControlFactory;

        /// <summary>
        /// edit control.
        /// </summary>
        public Func<object> EditControlFactory;

        private bool isComplex;
        private Button button;
        private ContentPresenter contentPresenter;


        /// <summary>
        /// Initializes the <see cref="DataField"/> class.
        /// </summary>
        static DataField()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof (DataField), new FrameworkPropertyMetadata(typeof (DataField)));
        }

        public DataField()
        {
            this.DefaultStyleKey = typeof (DataField);
        }

        public bool IsComplex
        {
            get { return this.isComplex; }
            set { this.isComplex = value; }
        }

        public bool IsInEditMode
        {
            get { return (bool) this.GetValue(IsInEditModeProperty); }
            set { this.SetValue(IsInEditModeProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.button = this.GetTemplateChild("Button") as Button;
            this.contentPresenter = this.GetTemplateChild("ContentPresenter") as ContentPresenter;
            if (button != null && this.contentPresenter != null)
            {
                this.Content = this.GenerateElement();
                this.button.Click += this.OnButtonClick;
                this.MouseDown += this.OnDataFieldMouseDown;
            }
        }

        void OnDataFieldMouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Content = this.GenerateEditElement();
            this.IsInEditMode = true;
        }

        void OnButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.IsInEditMode)
            {
                this.Content = this.GenerateElement();
                this.IsInEditMode = false;
            }
            else
            {
                this.Content = this.GenerateEditElement();
                this.IsInEditMode = true;
            }
        }

        public virtual object GenerateElement()
        {
            if (this.DisplayControlFactory != null)
            {
                return this.DisplayControlFactory.Invoke();
            }
            return null;
        }

        public virtual object GenerateEditElement()
        {
            if (this.EditControlFactory != null)
            {
                return this.EditControlFactory.Invoke();
            }
            return null;
        }
    }
}